using MES_workbench.Domian.Customers;
using MES_workbench.ErrorCode;
using Microsoft.Extensions.Logging;
using SqlSugar;
using WebApplication1.Implement.IServices;
using System;
using System.Collections.Generic;

namespace WebApplication1.Implement.Services
{
    /// <summary>
    /// 实现层类
    /// </summary>
    public class CustomerServices: ICustomerServices
    {
        private readonly SqlSugarClient _db;
        private readonly ILogger<CustomerServices> _logger;

        /// <summary>
        /// 构造函数注入 SqlSugarClient 和 日志
        /// </summary>
        /// <param name="db">SqlSugar 客户端</param>
        /// <param name="logger">日志记录器</param>
        public CustomerServices(SqlSugarClient db, ILogger<CustomerServices> logger)
        {
            _db = db;
            _logger = logger;
        }

        /// <summary>
        /// 获取工单工序明细数据列表
        /// </summary>
        /// <returns>ApiResult 包装的数据列表</returns>
        public ApiResult<List<WorkOrderProcess>> GetWorkOrderProcesses()
        {
            var res = new ApiResult<List<WorkOrderProcess>>();
            try
            {
                _logger.LogInformation("开始查询: {Entity}", nameof(WorkOrderProcess));
                var list = _db.Queryable<WorkOrderProcess>().ToList();
                res.success(list, "查询成功");
                _logger.LogInformation("查询成功: {Entity}, Count={Count}", nameof(WorkOrderProcess), list.Count);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询失败: {Entity}", nameof(WorkOrderProcess));
                res.msg = "查询失败";
            }
            return res;
        }

        /// <summary>
        /// 获取工单数据列表
        /// </summary>
        /// <returns>ApiResult 包装的数据列表</returns>
        public ApiResult<List<WorkOrder>> GetWorkOrders()
        {
            var res = new ApiResult<List<WorkOrder>>();
            try
            {
                _logger.LogInformation("开始查询: {Entity}", nameof(WorkOrder));
                var list = _db.Queryable<WorkOrder>().ToList();
                res.success(list, "查询成功");
                _logger.LogInformation("查询成功: {Entity}, Count={Count}", nameof(WorkOrder), list.Count);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询失败: {Entity}", nameof(WorkOrder));
                res.msg = "查询失败";
            }
            return res;
        }

        /// <summary>
        /// 获取报工数据列表
        /// </summary>
        /// <returns>ApiResult 包装的数据列表</returns>
        public ApiResult<List<WorkReport>> GetWorkReports()
        {
            var res = new ApiResult<List<WorkReport>>();
            try
            {
                _logger.LogInformation("开始查询: {Entity}", nameof(WorkReport));
                var list = _db.Queryable<WorkReport>().ToList();
                res.success(list, "查询成功");
                _logger.LogInformation("查询成功: {Entity}, Count={Count}", nameof(WorkReport), list.Count);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询失败: {Entity}", nameof(WorkReport));
                res.msg = "查询失败";
            }
            return res;
        }
    }
}
